package Model;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Database.*;
import Helper.*;

public class AdminM extends Model{
    private ArrayList<Admin> cache = null;

    public Admin login(String name, String password){
        AdminM adminM = new AdminM();
        ArrayList<Admin> temp = adminM.find(name);

        Admin result = null;

        if( temp.size() == 1 ){
            Admin u = temp.get(0);

            if( u.name.equals(name) && u.getPassword().equals(Helper.md5(password)) ){
                u.setStatus(name, Helper.md5(password));

                result = u;
            }else{
                System.err.println("用户名或密码错误！");
            }
        }else{
            System.err.println("用户名不存在！");
        }

        return result;
    }

    private ArrayList<Admin> all(){
        super.all("admin");
        // result is cached

        return refine();
    }

    private ArrayList<Admin> find(int id){
        super.find("admin",id);
        // result is cached

        return refine();
    }

    private ArrayList<Admin> find(String name){
        AdminDB db = new AdminDB();

        CachedRowSetImpl res = db.match(name);

        return createadminArr(res);
    }

    protected void init(String tableName){
        // override
        AdminDB db = new AdminDB();

        CachedRowSetImpl res = db.get(tableName);

        cache = createadminArr(res);
    }

    private ArrayList<Admin> createadminArr(CachedRowSetImpl res){
        // override

        ArrayList<Admin> result = new ArrayList<>();

        try {
            while (res.next()) {
                Admin u = new Admin(res.getString(2),res.getString(3));
                u.id = res.getInt(1);

                result.add(u);
            }
        } catch (SQLException exception) {
            System.err.println("数据库出错！");
            if( Helper.isDev ){
                exception.printStackTrace();
            }
        }

        return result;
    }

    private ArrayList<Admin> refine(){
        // same as User's

        ArrayList<Admin> target = new ArrayList<>();

        for( Admin item : cache ){
            Admin temp = new Admin(item.name,item.getPassword());
            temp.id = item.id;

            target.add(temp);
        }

        return target;
    }
}

class AdminDB extends DB{
    CachedRowSetImpl match(String name){
        CachedRowSetImpl cachedRowSet = null;

        try{
            connect();

            PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM admin WHERE name=?");
            statement.setString(1, name);

            ResultSet res = statement.executeQuery();

            cachedRowSet = new CachedRowSetImpl();
            cachedRowSet.populate(res);
        }catch(SQLException exception){
            System.err.println("数据库出错！");
            if( Helper.isDev ){
                exception.printStackTrace();
            }
        }finally {
            close();
        }

        return cachedRowSet;
    }
}

